#pragma once

#include <string>
#include <memory>

#include <rtc_base/rtc_certificate.h>

#include "base/event_loop.h"
#include "session_description.h"
#include "pc/transport_controller.h"


namespace xrtc {

// 可选择是否推送音频或者视频    
struct RTCOfferAnswerOptions {
    bool send_audio = true;
    bool send_video = true;
    bool recv_audio = true;
    bool recv_video = true;
    bool use_rtp_mux = true;    // 音视频通道是否复用 （是否采用BUDLE模式）
    bool use_rtcp_mux = true;   // rtp和rtcp复用一条通道传输
    bool dtls_on = true;        // 是否开启dtls加密
};    
    
class PeerConnection : public sigslot::has_slots<> {
public:
    PeerConnection(EventLoop* el, PortAllocator* allocator) : m_el(el),  
                m_transport_controller(new TransportController(el, allocator)) 
    {
        m_transport_controller->signal_candidate_allocate_done.connect(this,
            &PeerConnection::on_candidate_allocate_done);
    }

    ~PeerConnection() = default;

    int init(rtc::RTCCertificate* certificate);

    // 由用户参数的参数来设置对应的属性值
    std::string createOffer(const RTCOfferAnswerOptions& options);

    // 设置远端的SDP
    int set_remote_sdp(const std::string& sdp);

private:
    // 最终处理ICE Candidate收集完成后的回调函数
    void on_candidate_allocate_done(TransportController* transport_controller,
            const std::string& transport_name,
            IceCandidateComponent component,
            const std::vector<Candidate>& candidate);    

private:
    EventLoop* m_el;
    std::unique_ptr<SessionDescription> m_local_description;
    std::unique_ptr<SessionDescription> m_remote_description;
    rtc::RTCCertificate* m_certificate = nullptr; // 证书对象
    std::unique_ptr<TransportController> m_transport_controller; // 传输通道的管理对象
}; 

} // namespace xrtc
